package com.api.workflow.action;

import cn.hutool.json.JSONUtil;
import com.api.workflow.util.CrmUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

// 修改退货申请单
public class ModifyReturnApplicationAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try{
            // 表单名称
            String tableName = requestInfo.getRequestManager().getBillTableName();
            // 表单数据ID
            int bill_id = requestInfo.getRequestManager().getBillid();

            String sql = "select * from "+ tableName +" where id = "+ bill_id;
            new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> sql:"+ sql);
            RecordSet recordSet = new RecordSet();
            recordSet.execute(sql);
            // crm 对应 oa字段
            HashMap<String, String> mainTableInfoMap = new HashMap<String, String>();
            mainTableInfoMap.put("oa_status__c", "zt");  // 审批状态
//        mainTableInfoMap.put("approval_node__c", "nodename");  // 审批节点
//        mainTableInfoMap.put("approval_opinion__c", "");  // 审批意见
            mainTableInfoMap.put("reason_investigation_resul__c", "yydcjg");  // 原因调查结果
            mainTableInfoMap.put("return_exchange_reasonabil__c", "thhhlx");  // 退（换）货合理性
            mainTableInfoMap.put("return_handling_solution__c", "thhwclfa");  // 退回货物处理方案
            mainTableInfoMap.put("remark__c", "bz");  // 备注
            mainTableInfoMap.put("logistics_fee_in_out__c", "ftwlysfy");  // 发、退物流运输费（元）
            mainTableInfoMap.put("rework_cost_raw_materials__c", "fgclyflshfy");  // 返工处理-原辅料损耗费(元)
            mainTableInfoMap.put("rework_cost__c", "fgclrgfy");  // 返工处理-人工费(元)
            mainTableInfoMap.put("rework_cost_raw__c", "fgclyflztrfy");  // 返工处理-原辅料再投入费元
            mainTableInfoMap.put("rework_handling_energy_fee__c", "fgclynfy");  // 返工处理-用能费(元)
            mainTableInfoMap.put("rework_cost_hazardous_wast__c", "fgclwfclfy");  // 返工处理-危废处理费(元)
            mainTableInfoMap.put("total_amount__c", "hjy");  // 合计(元)
            mainTableInfoMap.put("note_2__c", "bz2");  // 财务备注
            mainTableInfoMap.put("processing_completed_time__c", "clwcsj");  // 处理完成时间
            mainTableInfoMap.put("is_ready_for_re_sale__c", "sfddzxxshsyzt");  // 是否达到再行销售货使用状态
            mainTableInfoMap.put("processing_confirmation_no__c", "clqrbz");  // 处理确认备注

            mainTableInfoMap.put("_id", "crmid");
            if (recordSet.next()){
                HashMap<String, String> param = new HashMap<String, String>();
                for (Map.Entry<String, String> stringStringEntry : mainTableInfoMap.entrySet()) {
                    String key = stringStringEntry.getKey();
                    String value = stringStringEntry.getValue();
                    String string = recordSet.getString(value);
                    new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> key："+ key +  " value："+ value + " string："+ string);
                    param.put(key, string);
                }
                String token = CrmUtil.getToken();

                String remark = requestInfo.getRequestManager().getRemark(); //流程意见
                param.put("approval_opinion__c", remark);
                String requestid = requestInfo.getRequestid(); // 请求ID
                RecordSet re = new RecordSet();
                re.executeQuery(" select nodename from workflow_nodebase where id = (select currentnodeid from workflow_requestbase where requestid = ?)",requestid);
                if (re.next()){
                    param.put("approval_node__c", re.getString("nodename"));
                }
                new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> param："+ JSONUtil.toJsonStr(param));
                boolean b = CrmUtil.updateThOrder(token,param);
                if (b){
                    new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> 修改退货申请单状态成功");
                }else {
                    new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> 修改退货申请单状态失败");
                    requestInfo.getRequestManager().setMessageid("11111"+requestInfo.getRequestid()+"22222");
                    requestInfo.getRequestManager().setMessagecontent("修改退货申请单状态失败("+ requestid +"):"+param.get("errorMsg"));
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
        }catch (Exception e){
            new BaseBean().writeLog("ModifyReturnApplicationAction ------------------------> e:"+  e);
        }
        return null;
    }
}
